#include "MenuLayer.h"
#include "GameScene.h"
using namespace cocos2d;

MenuLayer::MenuLayer()
	:_sound(0)
	,_music(0)
{
}

MenuLayer::~MenuLayer()
{
}

bool MenuLayer::init()
{
	if(CCLayer::init()){
		createBackground();
		createMenu();
		return true;
	}
	return false;
}

void MenuLayer::createBackground()
{
	CCLayerColor* backgroundClr=CCLayerColor::create(ccc4(0,0,0,128));
	addChild(backgroundClr);
}

void MenuLayer::setSoundAndMusicVolume(float soundVolume,float musicVolume)
{
	bool soundFlag=soundVolume>0;
	bool musicFlag=musicVolume>0;
	_sound->setSelectedIndex(soundFlag);
	_music->setSelectedIndex(musicFlag);
}

void MenuLayer::resume(CCObject* pSender)
{
	GameScene* gameScene=static_cast<GameScene*>(getParent());
	gameScene->resume();
}

void MenuLayer::sound(CCObject* pSender)
{
	GameScene* gameScene=static_cast<GameScene*>(getParent());
	gameScene->sound();
}

void MenuLayer::music(CCObject* pSender)
{
	GameScene* gameScene=static_cast<GameScene*>(getParent());
	gameScene->music();
}

void MenuLayer::createMenu()
{
	int fontSize(32);
	CCString* fontName=CCString::create("Thonburi");

	CCMenuItemLabel* resumeLabel=CCMenuItemLabel::create(CCLabelTTF::create("Resume",
		fontName->getCString(),fontSize),this,menu_selector(MenuLayer::resume));

	//CCMenuItemLabel* mainMenu = CCMenuItemLabel::create(CCLabelTTF::create("Main Menu",
	//	fontName->getCString(), fontSize), this, menu_selector(MenuLayer::mainMenu));

	CCMenuItemLabel* soundOn = CCMenuItemLabel::create(CCLabelTTF::create("Sound On", fontName->getCString(), fontSize));
	CCMenuItemLabel* soundOff = CCMenuItemLabel::create(CCLabelTTF::create("Sound Off", fontName->getCString(), fontSize));
	_sound = CCMenuItemToggle::createWithTarget(this, menu_selector(MenuLayer::sound), soundOff, soundOn, NULL);

	CCMenuItemLabel* musicOn = CCMenuItemLabel::create(CCLabelTTF::create("Music On", fontName->getCString(), fontSize));
	CCMenuItemLabel* musicOff = CCMenuItemLabel::create(CCLabelTTF::create("Music Off", fontName->getCString(), fontSize));
	_music = CCMenuItemToggle::createWithTarget(this, menu_selector(MenuLayer::music), musicOff, musicOn, NULL);

	//CCMenuItemLabel* reset = CCMenuItemLabel::create(CCLabelTTF::create("Reset",
	//	fontName->getCString(), fontSize), this, menu_selector(MenuLayer::reset));

	CCMenu* menu = CCMenu::create(resumeLabel, /*mainMenu,*/ _sound, _music, /*reset,*/ NULL);
	menu->alignItemsVerticallyWithPadding(10);
	addChild(menu);
}
